﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MeTalker.Model;
using MeTalker.BLL;
using System.Data;

public partial class Default_UserForm : WebForm
{
    string id = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["param"];
        if (!Page.IsPostBack) {
            if (!string.IsNullOrEmpty(id)) {
                BindData(id);
            }
        }
    }

    void BindData(string id) {
        UsersMod user = UsersBLL.GetUserByIdToMod(int.Parse(id));
        if (user != null)
        {
            txtUserName.Text = user.UserName;
            txtUserName.Enabled = false;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtUserName.Text))
        {
            Alert("用户名不能为空!");
            return;
        }
        if (string.IsNullOrEmpty(txtPass1.Text))
        {
            Alert("密码不能为空!");
            return;
        }
        if (!txtPass1.Text.Equals(txtPass2.Text))
        {
            Alert("两次密码不一致!");
            return;
        }
       
        UsersMod user = new UsersMod();
        user.UserName = txtUserName.Text;
        user.Password = base.GetMD5(txtPass1.Text);

        if (string.IsNullOrEmpty(id))
        {
            DataTable dt = UsersBLL.GetUser(txtUserName.Text);
            if (dt != null && dt.Rows.Count > 0)
            {
                Alert("该用户已经存在!");
                return;
            }

            if (UsersBLL.Insert(user) > 0)
            {
                Alert("添加用户成功!", "closePanel");
            }
            else
            {
                Alert("添加用户失败!");
            }
        }
        else {
            user.AutoID = int.Parse(id);
            if (UsersBLL.Update(user) > 0)
            {
                Alert("用户信息修改成功!", "closePanel", "RefreshData");
            }
            else
            {
                Alert("用户信息修改失败!");
            }
        }
    }
}